<?php
namespace app\we\controller;
use think\Controller;
use app\we\sdk\Thinkwechat;


class Index extends Controller{


    /**
     * 微信消息接口入口
     * 所有发送到微信的消息都会推送到该操作
     * 所以，微信公众平台后台填写的api地址则为该操作的访问地址
     * 所有的响应都有机器人响应
     * $this 498944516@qq.com
     */
    public function index($appid=0){
     
        //获取配置连接微信
        $this->appid=$appid;
        $weObj=weObj($appid);
        $res=$weObj->valid();

        //分解数据获得常用字段
        $this->openid = $weObj->getRev()->getRevFrom();
        $this->type = $weObj->getRev()->getRevType();
        $this->data = $weObj->getRev()->getRevData();
       
        //用户检测，如果有就存入data,没有则存入数据库
        $fans=model('WeFans')->info($this->openid);

        if (!$fans){
           $fans=getUserInfo($this->appid,$this->openid );

           if ($fans){
            model('WeFans')->editData($fans); 
            $fans=model('WeFans')->info($this->openid);
           }else{
            //todo 报警
           }
        }


       
        //补充常用相关数据到DATA
        $this->data['aid']=$weObj->options['aid'];
        $this->data['appid']=$weObj->options['appid'];
        $this->data['openid']=$fans['openid'];
        $this->data['userid']=$fans['userid'];
        $this->data['nickname']=$fans['nickname'];
        $this->data['options']=$weObj->options;
        $this->data['fans']=$fans;


        model('WeMessage')->saveMessage($this->data); //记录消息
        db('WeFans')->where('openid',$this->openid)->setField('update_time',time());  //记录心跳

        $message=$this->messageType();
        
        //处理兼容
        if (!is_array($message)){
            $reply['message']=$message;
        }else{
            $reply=$message;
        }
      
        if (!isset($reply['type'])) $reply['type']='text';
        $reply['message']=isset($reply['message'])?$reply['message']:'请记住代码和我爱着你';

        //回复粉丝
        $weObj->$reply['type']($reply['message'])->reply();

    }

    // 处理微信消息总入口
    public function messageType(){
       
        switch($this->type) {
            case Thinkwechat::MSGTYPE_TEXT:
                   $reply=model('rob/RobAnswer')->info($this->data['Content'],$this->data);
                    break;
            case Thinkwechat::MSGTYPE_IMAGE:
                   $reply=model('rob/RobAnswer')->info($this->type,$this->data,1);  //根据类型进准匹配
                    break;
            case Thinkwechat::MSGTYPE_VOICE:
                   //TODO::语音识别
                   $reply=model('rob/RobAnswer')->info($this->type,$this->data,1);
                    break;
            case Thinkwechat::MSGTYPE_MUSIC:
                   
                    break;
            
            case Thinkwechat::MSGTYPE_VIDEO:
                  
                    break;
            case Thinkwechat::MSGTYPE_LOCATION:
                  
                    break;
            case Thinkwechat::MSGTYPE_LINK:
                  
                    break;

            case Thinkwechat::MSGTYPE_EVENT:
                  $reply=$this->messageEvent();
                    break;
            default: 
                 $reply=['type'=>'text','message'=>'success'];
                  break;
                    
        }

        return $reply;

    }

    // 处理微信事件消息
    public function messageEvent(){
       
        switch($this->data['Event']){
            case Thinkwechat::EVENT_SUBSCRIBE:
                //记录扫码获取机器人回答
                $res=db('WeFans')->where('openid',$this->openid)->update(['subscribe'=>1,'subscribe_time'=>time()]);
                $answer_id=model('WeSceneScan')->scan($this->data);
                $reply=model('rob/RobAnswer')->info($answer_id,$this->data,2);
                break;
            case Thinkwechat::EVENT_UNSUBSCRIBE:
                $res=db('WeFans')->where('openid',$this->openid)->update(['subscribe'=>0,'unsubscribe_time'=>time()]);
                $reply=['type'=>'text','message'=>'success'];
                break;
            case Thinkwechat::EVENT_SCAN:
                 //记录扫码获取机器人回答
                $answer_id=model('WeSceneScan')->scan($this->data);
                $reply=model('rob/RobAnswer')->info($answer_id,$this->data,2);
              
                 break;
            case Thinkwechat::EVENT_LOCATION:
               
                break;
            case Thinkwechat::EVENT_MENU_CLICK:
                $reply=model('rob/RobAnswer')->info($this->data['EventKey'],$this->data,2);
                break;
            case Thinkwechat::EVENT_MENU_SCAN_PUSH:
                 
                break;
            case Thinkwechat::EVENT_MENU_SCAN_WAITMSG:
               
                
                break;
            case Thinkwechat::EVENT_MENU_PIC_SYS:
                
                break;
            case Thinkwechat::EVENT_MENU_PIC_PHOTO:
               
                break;
            case Thinkwechat::EVENT_MENU_PIC_WEIXIN:
               
                break;
            case Thinkwechat::EVENT_MENU_LOCATION:
               
                break;
            case Thinkwechat::EVENT_SEND_MASS:
                $reply['message']='success';
                break;
            case Thinkwechat::EVENT_SEND_TEMPLATE:
                //模板消息发送成功
                $map['ToUserName']=$this->data['FromUserName'];
                $map['FromUserName']='templateMessage';
                $message=db('WeMessage')->where($map)->order('CreateTime desc')->find();
                $res=db('WeMessage')->where(['id'=>$message['id']])->setField('status',1);
                $reply['message']='success';
               
                break;
            case Thinkwechat::EVENT_KF_SEESION_CREATE:
                
                break;
            case Thinkwechat::EVENT_KF_SEESION_CLOSE:
                
                break;
            case Thinkwechat::EVENT_KF_SEESION_SWITCH:
                
                break;
            case Thinkwechat::EVENT_CARD_PASS:
                
                break;
            case Thinkwechat::EVENT_CARD_NOTPASS:
                
                break;
            case Thinkwechat::EVENT_CARD_USER_GET:
                
                break;
            case Thinkwechat::EVENT_CARD_USER_DEL:
                
                break;
            case Thinkwechat::WifiConnected :
                
                break;
            case Thinkwechat::ShakearoundUserShake :
                
                break;
            default:
                break;
           }

            return $reply;
        }

  

}